/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.animation
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	public class AnimatedContainer extends MovieClipConteiner
	{
		
		private var events:Vector.<AnimatedEvent>;
		
		private var isPlaying:Boolean;
		
		private var currentEvent:AnimatedEvent;
		
		public function AnimatedContainer()
		{
			super();
			isPlaying = false;			
			events = new Vector.<AnimatedEvent>();
		}
		
		override public function play(event:AnimatedEvent = null):void{			
			if(event){
				currentEvent = event;
				if(startAnimationFrame()){
					isPlaying = true;
					return;
				}	
			}
			
			if(events.length == 0){
				isPlaying = false;
				currentEvent = null;
			}else{
				
				while(events.length){
					currentEvent = events.shift();
					if(startAnimationFrame()){
						isPlaying = true;
						break;
					}else{
						isPlaying = false;
						currentEvent = null;
					}
				}
			}
			
		}
		
		private function animationComplete():void{
			isPlaying = false;	
			dispatchEvent(new AnimatedEvent(currentEvent.Label, currentEvent.Repeat, AnimatedEvent.ANIMATIONCOMPLETE));
			currentEvent = null;			
			play();
		}
		
		public function resume():void{
			if(currentEvent){
				isPlaying = true;
			}else{
				play();
			}
		}
		
		public function pause():void{
			isPlaying = false;
		}
		
		override public function stop(force:Boolean):void{
			if(force){
				isPlaying = false;
				currentEvent = null;
			}else{
				if(currentEvent){
					currentEvent.Repeat = 1;
				}
			}
		}
		
		override public function nextFrame():void{
			if(currentEvent){
				for each( var movieClip:AnimatedMovieClip in movieClips){						
					movieClip.nextFrameLabel(currentEvent.Label);						
				}
			}
		}
		
		override public function prevFrame():void{
			if(currentEvent){
				for each( var movieClip:AnimatedMovieClip in movieClips){						
					movieClip.prevtFrameLabel(currentEvent.Label);						
				}
			}
		}
		
		
		
		public function enterFrame():void{
			if(isPlaying){				
				if(currentEvent){
					switch(currentEvent.Type){
						
						case AnimatedEvent.PLAY_FORWARD:
							nextAnimationFrame();
							break;
						
						case AnimatedEvent.PLAY_BACKWARD:
							gotoAndStop(currentEvent.Label);
							prevAnimationFrame();
							break;
							
						case AnimatedEvent.GOTOANDSTOP:
							gotoAndStop(currentEvent.Label);
							animationComplete();
							break;
						
						case AnimatedEvent.NEXTFRAME:
							nextFrame();
							animationComplete();
							break;
							
						case AnimatedEvent.PREVFRAME:
							prevFrame();
							animationComplete();
							break;
					}
				}
			}
		}
		
		private function prevAnimationFrame():void{
			
			var completeAnimations:int = 0;
			
			for each( var movieClip:AnimatedMovieClip in movieClips){						
				if(!movieClip.prevtFrameLabel(currentEvent.Label)){
					if(currentEvent.Repeat > 0){
						//следующий цикл? 
						if(++currentEvent.RepeatCount < currentEvent.Repeat){
							//да следующий
							movieClip.gotoAndStop(currentEvent.Label);
						}else{
							//нет хватит
							++completeAnimations;
						}
					}else{
						//безконечный цикл
						movieClip.gotoAndStop(currentEvent.Label);
					}
				}
			}
			
			if(movieClips.length == completeAnimations){
				animationComplete();
			}
					
		}
		
		private function nextAnimationFrame():void{
					
			var completeAnimations:int = 0;
			
			for each( var movieClip:AnimatedMovieClip in movieClips){						
				if(!movieClip.nextFrameLabel(currentEvent.Label)){
					if(currentEvent.Repeat > 0){
						//следующий цикл? 
						if(++currentEvent.RepeatCount < currentEvent.Repeat){
							//да следующий
							movieClip.gotoAndStop(currentEvent.Label);
						}else{
							//нет хватит
							++completeAnimations;
						}
					}else{
						//безконечный цикл
						movieClip.gotoAndStop(currentEvent.Label);
					}
				}
			}
			
			if(movieClips.length == completeAnimations){
				animationComplete();
			}
					
		}
		
		public function startAnimationFrame():Boolean{
			var result:Boolean = true;
			if(currentEvent){
				for each( var movieClip:AnimatedMovieClip in movieClips){
					if(!movieClip.labelExist(currentEvent.Label)){
						result = false;
						break;
					}
				}
				if(result){
					for each( var movieClipToPlay:AnimatedMovieClip in movieClips){
						movieClipToPlay.gotoAndStop(currentEvent.Label);
					}
				}
				
			}else{
				result = false;
			}
			
						
			return result;
		}
		
		public function get playing():Boolean{
			return isPlaying;
		}
		
		public function removeEvents():void{			
			events = new Vector.<AnimatedEvent>();
			stop(true);
		}
		
		
		
		public function addEvent(value:AnimatedEvent):void{			
			events.push(value);
		}
	}
}